Chương trình mã hóa giấu tin trong ảnh bằng C#
6.557 lượt xem;
- CryptoHelper.cs
- SteganographyBMP /
- SteganographyBMP /
- project /
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using System.Security.Cryptography;
7
8 namespace SteganographyBMP
9 {
10 class CryptoHelper
11 {
12 public static byte[] Encrypt(byte[] message,string password)
13 {
14 return CommonMethod(message, password);
15 }
16
17 public static byte[] Decrypt(byte[] message, string password)
18 {
19 return CommonMethod(message, password);
20 }
21
22 // ham su dung chung cho ca viec ma hoa lan giai ma
23 private static byte[] CommonMethod(byte[] message, string password)
24 {
25 byte[] salt = { 35, 1, 76 }; // dung cho viec sinh khoa
26 PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt);
27 byte[] key = pdb.GetBytes(128); // dua doi tuong pdb ve dang khoa 128 bit
28 byte[] retMessage = new byte[message.Length];
29 for (int i = 0; i < message.Length; i++)
30 {
31 int index = i % key.Length; // vi chieu dai cua thong diep co the lon hon chieu dai cua khoa
32 retMessage[i] = (Byte)(key[index] ^ message[i]); // thuc hien phep XOR , muc dich la sau khi thuc hien phep XOR 2 lan ta se duoc nguyen ban
33 }
34
35 return retMessage;
36 }
37 }
38 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using System.Security.Cryptography;
7
8 namespace SteganographyBMP
9 {
10 class CryptoHelper
11 {
12 public static byte[] Encrypt(byte[] message,string password)
13 {
14 return CommonMethod(message, password);
15 }
16
17 public static byte[] Decrypt(byte[] message, string password)
18 {
19 return CommonMethod(message, password);
20 }
21
22 // ham su dung chung cho ca viec ma hoa lan giai ma
23 private static byte[] CommonMethod(byte[] message, string password)
24 {
25 byte[] salt = { 35, 1, 76 }; // dung cho viec sinh khoa
26 PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt);
27 byte[] key = pdb.GetBytes(128); // dua doi tuong pdb ve dang khoa 128 bit
28 byte[] retMessage = new byte[message.Length];
29 for (int i = 0; i < message.Length; i++)
30 {
31 int index = i % key.Length; // vi chieu dai cua thong diep co the lon hon chieu dai cua khoa
32 retMessage[i] = (Byte)(key[index] ^ message[i]); // thuc hien phep XOR , muc dich la sau khi thuc hien phep XOR 2 lan ta se duoc nguyen ban
33 }
34
35 return retMessage;
36 }
37 }
38 }